package dao_sys

import (
	"wrblog-api-go/app/model"
	"wrblog-api-go/app/model/model_sys"
	"wrblog-api-go/pkg/errs"
	"wrblog-api-go/pkg/mysql"
	"wrblog-api-go/pkg/utils"
)

func GetSysUserPageList(sysUser model_sys.SelectSysUser) (sysUsers []model_sys.SysUserVo, total int64, err error) {
	db := mysql.Db().Model(&model_sys.SysUserPo{})
	if sysUser.Uk != "" {
		db = db.Where("uk like ?", "%"+sysUser.Uk+"%")
	}
	if sysUser.NickName != "" {
		db = db.Where("nick_name like ?", "%"+sysUser.NickName+"%")
	}
	if sysUser.SpaceUrl != "" {
		db = db.Where("space_url like ?", "%"+sysUser.SpaceUrl+"%")
	}
	if sysUser.Mobile != "" {
		db = db.Where("mobile like ?", "%"+sysUser.Mobile+"%")
	}
	if sysUser.Mailbox != "" {
		db = db.Where("mailbox like ?", "%"+sysUser.Mailbox+"%")
	}
	if sysUser.Sex != "" {
		db = db.Where("sex = ?", sysUser.Sex)
	}
	if sysUser.RegisterType != "" {
		db = db.Where("register_type = ?", sysUser.RegisterType)
	}
	if sysUser.UserType != "" {
		db = db.Where("user_type = ?", sysUser.UserType)
	}
	if sysUser.Status != "" {
		db = db.Where("status = ?", sysUser.Status)
	}
	if !sysUser.StartTime.IsZero() {
		db = db.Where("create_time >= ?", sysUser.StartTime)
	}
	if !sysUser.EndTime.IsZero() {
		db = db.Where("create_time <= ?", sysUser.EndTime)
	}
	db = db.Where("deleted = '0'").Count(&total).Order("create_time desc")
	if sysUser.PageNum != nil && sysUser.PageSize != nil {
		db = db.Limit(*sysUser.PageSize).Offset((*sysUser.PageNum - 1) * *sysUser.PageSize)
	} else {
		db = db.Limit(10)
	}
	err = db.Find(&sysUsers).Error
	err = errs.ErrNotFound(err)
	return
}

func GetSysUserById(userId int) (sysUser model_sys.SysUserVo, err error) {
	db := mysql.Db().Model(&model_sys.SysUserPo{})
	err = db.Where("user_id = ? and deleted = '0'", userId).First(&sysUser).Error
	err = errs.ErrNotFound(err)
	return
}

func AddSysUser(sysUser model_sys.SaveSysUser) (string, error) {
	err := mysql.Db().Create(&sysUser).Error
	return sysUser.UserId, errs.ErrNotFound(err)
}

func EditSysUser(sysUser model_sys.SaveSysUser) (int64, error) {
	res := mysql.Db().Updates(&sysUser)
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}

func RemoveSysUser(userIds []string, uk string) (int64, error) {
	db := mysql.Db().Model(&model_sys.SysUserPo{}).Where("user_id in (?)", userIds)
	res := db.Updates(&model_sys.SysUserPo{BaseEntity: model.BaseEntity{UpdateTime: utils.NewTimeNow(), UpdateBy: uk, Deleted: "1"}})
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}

func DeleteSysUser(userIds []string) (int64, error) {
	db := mysql.Db().Model(&model_sys.SysUserPo{})
	res := db.Delete(&model_sys.SysUserPo{}, userIds)
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}

func GetUserByUk(uk string) (sysUser model_sys.SysUserPo, err error) {
	err = mysql.Db().Model(&model_sys.SysUserPo{}).Where("uk = ? and deleted = '0'", uk).First(&sysUser).Error
	err = errs.ErrNotFound(err)
	return
}

func GetUserBySpaceUrl(spaceUrl string) (sysUser model_sys.SysUserPo, err error) {
	err = mysql.Db().Model(&model_sys.SysUserPo{}).Where("space_url = ? and deleted = '0'", spaceUrl).First(&sysUser).Error
	err = errs.ErrNotFound(err)
	return
}

func GetUserByMk(mk string) (sysUser model_sys.SysUserPo, err error) {
	err = mysql.Db().Model(&model_sys.SysUserPo{}).Where("mobile = ? and deleted = '0'", mk).First(&sysUser).Error
	err = errs.ErrNotFound(err)
	return
}

func GetUserByMbk(mbk string) (sysUser model_sys.SysUserPo, err error) {
	err = mysql.Db().Model(&model_sys.SysUserPo{}).Where("mailbox = ? and deleted = '0'", mbk).First(&sysUser).Error
	err = errs.ErrNotFound(err)
	return
}

func GetUserById(userId int64) (sysUser model_sys.SysUserVo, err error) {
	err = mysql.Db().Model(&model_sys.SysUserPo{}).Where("user_id = ? and deleted = '0'", userId).First(&sysUser).Error
	err = errs.ErrNotFound(err)
	return
}
